package com.ifmvo.togetherad.core.helper

import android.app.Activity
import android.content.Context
import com.ifmvo.togetherad.core.R
import com.ifmvo.togetherad.core.TogetherAd
import com.ifmvo.togetherad.core.config.AdProviderLoader
import com.ifmvo.togetherad.core.listener.RewardListener
import com.ifmvo.togetherad.core.provider.BaseAdProvider
import com.ifmvo.togetherad.core.utils.DispatchUtil
import com.ifmvo.togetherad.core.utils.loge
import com.xinyue.sdk.utilcode.util.ActivityUtils
import com.xinyue.sdk.utilcode.util.Utils
import org.jetbrains.annotations.NotNull
import java.lang.ref.WeakReference

/**
 * 激励广告
 *
 * Created by Matthew Chen on 2020-04-20.
 */
class AdHelperReward(
    @NotNull alias: String,
    ratioMap: LinkedHashMap<String, Int>? = null,
    listener: RewardListener? = null

) : BaseHelper() {

    private lateinit var mContext: WeakReference<Context>
    private var mAlias: String = alias
    private var mRatioMap: LinkedHashMap<String, Int>? = ratioMap
    private var mListener: RewardListener? = listener
    private var adProvider: BaseAdProvider? = null
    private var mLoadAndShow: Boolean = false

    //为了照顾 Java 调用的同学
    constructor(
        @NotNull alias: String,
        listener: RewardListener? = null
    ) : this(alias, null, listener)

    fun load(context: Context?) {
        load(context, false)
    }

    fun load(context: Context?, loadAndShow: Boolean = false) {
        mLoadAndShow = loadAndShow
        var realContext: Context = context ?: ActivityUtils.getTopActivity() ?: Utils.getApp()
        mContext = WeakReference(realContext)
        val currentRatioMap: LinkedHashMap<String, Int> =
            if (mRatioMap?.isEmpty() != false) TogetherAd.getPublicProviderRatio() else mRatioMap!!

        startTimer(mListener)
        reload(currentRatioMap)
    }


    private fun reload(@NotNull ratioMap: LinkedHashMap<String, Int>) {

        val adProviderType = DispatchUtil.getAdProvider(mAlias, ratioMap)

        if (adProviderType?.isEmpty() != false || mContext.get() == null) {
            cancelTimer()
            mListener?.onAdFailedAll(FailedAllMsg.failedAll_noDispatch)
            return
        }

        adProvider = AdProviderLoader.loadAdProvider(adProviderType)

        if (adProvider == null) {
            "$adProviderType ${mContext.get()?.getString(R.string.no_init)}".loge()
            reload(filterType(ratioMap, adProviderType))
            return
        }

        adProvider?.requestRewardAd(
            mContext.get(),
            adProviderType,
            mAlias,
            object : RewardListener {
                override fun onAdStartRequest(providerType: String) {
                    mListener?.onAdStartRequest(providerType)
                }

                override fun onAdFailed(providerType: String, failedMsg: String?) {
                    if (isFetchOverTime) return

                    reload(filterType(ratioMap, adProviderType))

                    mListener?.onAdFailed(providerType, failedMsg)
                }

                override fun onAdClicked(providerType: String) {
                    mListener?.onAdClicked(providerType)
                }

                override fun onAdShow(providerType: String) {
                    mListener?.onAdShow(providerType)
                }

                override fun onAdLoaded(providerType: String) {
                    if (isFetchOverTime) return
                    cancelTimer()
                    isLoadOK = true
                    if (mLoadAndShow) {
                        adProvider?.showRewardAd(mContext.get())
                        isLoadOK = false
                    }
                    mListener?.onAdLoaded(providerType)
                }

                override fun onAdExpose(providerType: String) {
                    mListener?.onAdExpose(providerType)
                }

                override fun onAdVideoComplete(providerType: String) {
                    mListener?.onAdVideoComplete(providerType)
                }

                override fun onAdVideoCached(providerType: String) {
                    mListener?.onAdVideoCached(providerType)
                }

                override fun onAdRewardVerify(providerType: String) {
                    mListener?.onAdRewardVerify(providerType)
                }

                override fun onAdClose(providerType: String) {
                    mListener?.onAdClose(providerType)
                }
            })
    }

    fun show(context: Context?): Boolean {

        if (adProvider == null || !isLoadOK) {
            load(context, true)
        }

        return adProvider!!.showRewardAd(context)
    }


}